#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

#define DEV_NAME "/dev/ttyS1"

int testi=0;


// 等待时间  串口描述符  读取的buff
int Select_Tart_Read(struct timeval tv,int  uart_fd,char* buff)
{
    
    memset(buff,0,80);
    fd_set rfds;
    int retval=0;
    int i;
    
    FD_ZERO(&rfds);
    FD_SET(uart_fd, &rfds);
    
    retval=select(uart_fd + 1, &rfds, NULL, NULL, &tv);
 // retval=select(uart_fd + 1, &rfds, NULL, NULL, NULL);
    if(retval<0)
    {  
        perror("select error\n");  
    }
    else
    {  
        if( retval &&   FD_ISSET(uart_fd, &rfds)  )
        {
            testi++;
            printf("FD_ISSET!6\n");
            int rc=read(uart_fd,buff,8);
          
          printf("FD_ISSET!3\n");
            if(rc>0)
            {
                printf("recv serial----rc is %d   testi is %d \n  ",rc,testi);
              //  for(i = 0; i < 80; i++)
              //  printf(" %x ", buff[i]);
              //  printf("\n");
                return rc;
            }
        }    
    }    
    return 0;
}


int monitor_routine( int  uart_fd)
{
    int i;
    printf("in  monitor_routine\n");
    
    char rebuff[80];//设置最大的数据长度为8个
    memset(rebuff,0,80);
    struct timeval tv={0,3};
    struct timeval timeout={0,3}; //select等待3微秒，3微秒轮询，要非阻塞就置0
   // tv.tv_sec = 0;
  //  tv.tv_usec = 10000;
    int retval;
  //  unsigned char commbuff[8];//设置最大的数据长度为8个
   // memset(commbuff,0,sizeof(unsigned char)*8);
    
    while(1)
    {
        //printf("retval is %d\n",retval);
        if(Select_Tart_Read(tv,uart_fd,rebuff)>0)//
        {
            printf("in while Read!\n");
            for(i = 0; i < 80; i++)
		{
                printf(" %x ", rebuff[i]);
                rebuff[i]=0;
		}
            printf("\n");
        }
    }
    return 0;
}


int main(int argc, char *argv[])
{
        int iFd, i;
        int len;
        unsigned char ucBuf[100];
        struct termios opt;

        iFd = open(DEV_NAME, O_RDWR|O_NOCTTY|O_NDELAY);
              
        if(iFd < 0) {
                perror(DEV_NAME);
                printf(" open /dev/ttyS1 faurel!\n");
                return -1;
        }
        else 
            printf(" open /dev/ttyS1 !\n");
        tcgetattr(iFd, &opt);
        cfsetispeed(&opt, B115200);
        cfsetospeed(&opt, B115200);

        if (tcgetattr(iFd,   &opt)<0) {
                  return   -1;
        }
        opt.c_lflag    &= ~(ECHO | ICANON | IEXTEN | ISIG);
        opt.c_iflag    &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
        opt.c_oflag     &= ~(OPOST);
        opt.c_cflag     &= ~(CSIZE | PARENB);
        opt.c_cflag     |=  CS8;

        opt.c_cc[VMIN]     = 255;
        opt.c_cc[VTIME]    = 150;

        if (tcsetattr(iFd,   TCSANOW,   &opt)<0) {
                return   -1;
        }
        tcflush(iFd,TCIOFLUSH);
        
        
        for (i = 0; i < 100; i++){
                ucBuf[i] = 0xff - i;
        }
        
        
        char buff[6];
        buff[0]=0x05;buff[1]=0x05;buff[2]=0x05;buff[3]=0x05;buff[4]=0x05;buff[5]=0x05;
        
        
        printf("buff is :\n");
        for(i=0;i<6;i++)
            printf("0x%x ",buff[i]);
        printf("\n");
        
        printf("write buff!!!");
        
        len = write(iFd, buff, 6);
        
        if(len < 0) {
             
            printf(" write error ! \n");
            return -1;
        }
        
        printf("in main monitor_routine\n");
        
        monitor_routine(iFd);
        
        printf("\n");



        close(iFd);
        return 0;
}
